/**
 * 
 */
package neptune.web.client.view.components.editor;

import java.util.ArrayList;

import neptune.web.client.model.topology.SerializableMACAddress;
import neptune.web.client.model.topology.SerializableNetworkInterface;
import neptune.web.client.model.topology.SerializableNode;
import neptune.web.client.model.topology.SerializableTopology;
import neptune.web.client.utils.TopologyGenerationUtils;

import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.MenuBar;
import com.google.gwt.user.client.ui.TreeItem;

/**
 * @author Roberto Bifulco [RobertoBifulco.it] [info@robertoBifulco.it]
 * 
 */
public class AddNodeCommand implements Command {

	private SerializableTopology topology;
	private TreeItem item;
	private final TopologyEditor editor;

	public AddNodeCommand(SerializableTopology topology, TreeItem treeItem,
			TopologyEditor editor) {
		this.topology = topology;
		this.item = treeItem;
		this.editor = editor;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.google.gwt.user.client.Command#execute()
	 */
	public void execute() {
		final SerializableNode node = new SerializableNode();
		node.setId(TopologyGenerationUtils.generateNodeName(topology));
		node.setRam(256);
		node.setHd(2);
		topology.getNodes().add(node);

		final TreeItem nodeItem = new TreeItem();
		nodeItem.setWidget(new NodeMenuBar(node, topology, nodeItem, editor));
		item.addItem(nodeItem);

		TreeItem interfacesItem = new TreeItem();
		MenuBar interfacesLabel = new InterfacesMenuBar(node, topology,
				interfacesItem);
		interfacesItem.setWidget(interfacesLabel);
		nodeItem.addItem(interfacesItem);

		TreeItem softwaresItem = new TreeItem();
		MenuBar softwaresLabel = new SoftwaresMenuBar(node, softwaresItem);
		softwaresItem.setWidget(softwaresLabel);
		nodeItem.addItem(softwaresItem);

		final Label nodeGraphic = editor.createNodeGraphic(node);
		editor.getGraphPanel().add(nodeGraphic,
				(int) (350 + Math.random() * 100),
				(int) (250 + Math.random() * 100));
		editor.getGraphPanel().makeDraggable(nodeGraphic);

		addNetIterfaceToNode(node, nodeItem);

		NodePropsDialog dialog = new NodePropsDialog(node, nodeItem, editor);
		dialog.setCancelCallback(new CancelCallBack() {
			public void onCancel() {
				nodeItem.remove();
				topology.getNodes().remove(node);
				nodeGraphic.removeFromParent();
			}
		});
		dialog.center();
	}

	private void addNetIterfaceToNode(SerializableNode node, TreeItem nodeItem) {
		ArrayList<SerializableNetworkInterface> interfaces = node
				.getInterfaces();

		SerializableNetworkInterface netIf = new SerializableNetworkInterface();
		netIf.setInterfaceName("eth0");
		SerializableMACAddress mac = new SerializableMACAddress();
		mac.setAddress(TopologyGenerationUtils.generateRandomMAC());
		netIf.setMacAddress(mac);
		netIf.setBindingDescription(SerializableNetworkInterface.HIF);

		interfaces.add(netIf);
		node.setInterfaces(interfaces);

		new AddControlNetIFGraphicCommand(netIf, node, topology, nodeItem)
				.execute();
	}

}
